<?php
/*
 用户管理
 */
	namespace Admin\Controller;
	use Think\Controller;
	class UserController extends CommonController {

		//首页
		public function index()
		{
			header("content-type:text/html;charset=utf-8");
    //查询       
         if ($_GET['sub']=="查询") {
                    $key=$_GET['keywords'];
            $wheres['tel']=array('like',"%$key%");
            // $wheres['typeid']=$type;
            $wheres=array_filter($wheres); 
         }else{
            $wheres='';
         }

		$db=M("Userinfo"); // 实例化Plan对象
		$count      = $db->where($wheres)->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(15)
		$show       = $Page->show();// 分页显示输出// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$Uarr = $db->where($wheres)->order('createtime DESC')->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('Uarr',$Uarr);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
		$this->display(); // 输出模板
    }

// 修改
		public function upd()
		{
			$wheres['mid']=I("post.sid");
			$data['mstatus']=I("post.sta");
			if (IS_POST) {
				$db=M("Tixian"); 
				$list=$db->where($wheres)->save($data);
				if ($list) {
					echo "修改成功";
				}else{
					echo "修改失败";
				}
			}
			   
		}


		//增加用户
		public function insert()
		{  
			   
			if(!empty($_POST)){    
				    // 写入数据
		        $Form = D("Userinfo");
		        if ($vo = $Form->create()) {
		            $list = $Form->add();
		            if ($list !== false) {
		                $this->success('数据保存成功！');
		            } else {
		                $this->error('数据写入错误！');
		            }
		        }else{
		            $this->error($Form->getError());
		        }
			}else{
				$this->display();
			}

		}
		//修改
		public function userinfo()
		{
		$name="Userinfo";
		if (!empty($_POST)) {
			$this->_update($name);
		}else{
		  $this->_edit($name);
	  }
		}
    // 删除数据
    public function delete() {
    	      if ($_POST['action']=='delAll') {
    	      		$str=I('idstr');
    	      		if (empty($str)) {
    	      			echo "您未选择任何信息";
    	      			die;
    	      		}
        			$ids=substr($str,1);
					if (!empty($ids)) {
					$Form = M("Userinfo");
					$result = $Form->delete($ids);
					if (false !== $result) {
					echo '删除成功！';
					} else {
					echo '删除出错！';
					}
					} else {
					echo 'ID错误！';
					}
    	      	}else{
					$id=I('sid');
					if (!empty($id)) {
					$Form = M("Userinfo");
					$result = $Form->delete($id);
					if (false !== $result) {
					echo '删除成功！';
					} else {
					echo '删除出错！';
					}
					} else {
					echo 'ID错误！';
					}
    		}
   	 }

	// 邀请
	public function yaoqing($yid)
	{
		$User=M("Userinfo");
		$UserInfo=$User->where("uid=$yid")->find();
		$this->assign('vo',$UserInfo);
		$this->display();
	}
	//修改状态
	public function Update()
	{
		 $wheres['uid']=I("sid");
		 $status=I("sta");
		if (!empty($wheres)) {
			$User=M("Userinfo");
			if ($status==1) {
			$data['status']=0;
			}else{
			$data['status']=1;
			}
			$list=$User->where($wheres)->save($data);		
			if ($list) {
				echo "修改成功";
			}else{
				echo "修改失败";
			}

		}
	}

	}